/*
 * author:          可乐加糖
 * email:           black.angel.liu@gmail.com
 * description:     管理界面组件
 */
 
 
Ext.onReady(function(){
	
	var thisNode = null;
  	function sendAjax(url,node){
  		Ext.Ajax.request({
		   url: url,
		   success: function(response, opts) {
		      Ext.MessageBox.alert("恭喜","操作成功，数据已经同步至最新。");
		      node.reload();
		   },
		   failure: function(response, opts) {
		   	  Ext.MessageBox.alert("提示","操作失败，请重新操作或联系系统管理员。");
		   }
		});
  	}
  	
  	var contextMenu = new Ext.menu.Menu({
        id : 'rightClickCont',
        items : [{
            id : 'nodeAdd',
            text : '添加节点',
            handler : function() {
            	var node = treeNav.getSelectionModel().getSelectedNode();
            	Ext.Msg.prompt('添加节点', '请输入新节点的名称:', function(btn, text){
				    if (btn == 'ok'){
				    	var nodeName = text;
				        var url = encodeURI("./manager.do?method=createOrUpdate&fatherid="+thisNode+"&typename="+nodeName);
				        sendAjax(url,node);
				    }
				});
            }
        }, {
            id : 'nodeEdit',
            text : '编辑节点',
            handler : function() {
            	var node = treeNav.getSelectionModel().getSelectedNode();
            	Ext.Msg.prompt('编辑节点', '请输入节点的新名称:', function(btn, text){
				    if (btn == 'ok'){
				    	var nodeName = text;
				        var url = encodeURI("./manager.do?method=createOrUpdate&update=true&id="+node.id+"&typename="+nodeName+"&fatherid="+node.attributes.father);
				        sendAjax(url,node);
				    }
				});
            }
        }, {
            id : 'nodeDelete',
            text : '删除节点',
            handler : function(){
            	var node = treeNav.getSelectionModel().getSelectedNode();
            	Ext.Msg.confirm('删除节点', '确认删除节点吗？', function(btn, text){
				    if (btn == 'yes'){
				        var url = encodeURI("./manager.do?method=deleteById&typeid="+node.id);
				        node = node.parentNode;
				        sendAjax(url,node);
				    }
				});
            }
        }]
    });
	
	
	
	
	var rootNode = new Ext.tree.AsyncTreeNode({
        nodeType: 'async',
        text: '栏目列表',
        draggable: false,
        id: '0'
    })
	//树加载项
	var treeLoad = new Ext.tree.TreeLoader({
  		//第一次加载
  		dataUrl:'./manager.do?method=getAllType&id=0'
  	});
	//构建功能导航树
	var treeNav = new Ext.tree.TreePanel({
        useArrows: true,
        autoScroll: true,
        animate: true,
        enableDD: true,
        containerScroll: true,
        border: false,
        dataUrl:treeLoad,
        root: rootNode,
        listeners:({
        	beforeload:function(node){
        		treeNav.loader.dataUrl= './manager.do?method=getAllType&id='+node.id;    //及时更新URL
        	},
        	contextmenu:function(node,event){
        		event.preventDefault();// 阻止浏览器默认右键菜单显示
        		node.select();
        		//if(node.attributes.father==0||node.id==0){
        			contextMenu.showAt(event.getXY());// 取得鼠标点击坐标，展示菜单
	                //右键菜单
	                thisNode = node.id;
        		//}else{
        		//	return false;
        		//}
        	},click:function(node){
        		if(node.leaf){
        			document.getElementById("ifr-page").src = "./manager.do?method=findArticleByType&id="+node.id;
        			//document.getElementById("ifr-page").src = "http://www.baidu.com/";
        		}
        	}
        })
    });
    
    var rootNode2 = new Ext.tree.TreeNode({
    	text:'新闻项目管理'
    });
    
    var treeNode1 = new Ext.tree.TreeNode({
    	text:'新闻管理',
    	url:'manager.do?method=newsList',
    	id:'news'
    });
    var treeNode2 = new Ext.tree.TreeNode({
    	text:'公告管理',
    	url:'manager.do?method=TopicsList',
    	id:'topics'
    });
    rootNode2.appendChild(treeNode1);
    rootNode2.appendChild(treeNode2);
    
    var treeNav2 = new Ext.tree.TreePanel({
    	useArrows: true,
        autoScroll: true,
        animate: true,
        enableDD: true,
        containerScroll: true,
        border: false,
        //dataUrl:treeLoad,
        root: rootNode2,
        listeners:({
        	contextmenu:function(node,event){
        		event.preventDefault();// 阻止浏览器默认右键菜单显示
        	},click:function(node){
        		document.getElementById("ifr-page").src = node.attributes.url;
        	}
        })
    });
	
	//主面板
	new Ext.Viewport({
		layout:'border',
		items:[
			{
				xtype:'container',
				collapsible:false,
				height:68,
				bodyBorder:false,
				border:false,
				region:'north',
				html:'<div id="header" style="position:relative;"><img src="./images/manager/title.png" width="400" height="65" alt="系统管理" /><a style="position:absolute;top:20px;right:10px;font-size:12px;color:#fff;" href="./login.do?method=loginout">注销</a></div>'
			},{
				title:'',
				collapsible:false,
				region:'center',
				bodyBorder:false,
				border:false,
				layout:'fit',
				html:'<div id="page">' +
						'<iframe id="ifr-page" name="ifr-page" width="100%" height="100%" frameborder="0" scrolling="auto" src="" /></div>',      //默认页面
				margins:{
					bottom:3
				}
			},{
				title:'功能导航',
				width:220,
				maxWidth:300,
				minWidth:150,
				collapsible:true,
				split:true,
				region:'west',
				bodyBorder:'true',
				margins:{
					bottom:3
				},
				items:[treeNav2,treeNav]
			}
		]
	});
	//初始化一级分类
	treeNav.getRootNode().expand();
});